/*
 * Copyright [2011] [Ilya Rudakov]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var LogChecker = function() {
    this.log_checker_timer_id = null;
    this.feed_id_holder = "#feed_id_holder";
    this.start_byte_holder = "#start_byte_holder";
    this.logs_container = "#logs_container";
    this.current_log_file_name_holder = "#current_log_file_name_holder";
};

LogChecker.prototype = {
    refresh_logs: function() {
        var those = this;
        var feed_id = $(this.feed_id_holder).val();
        var log_file_name = $(this.current_log_file_name_holder).val();
        var start_byte = $(this.start_byte_holder).val();
        
        var data = {"feed_id":feed_id, "log_file_name":log_file_name, "start_byte":start_byte};

        $.getJSON("/jm/get_logs", data, function(response) {
            if($(those.start_byte_holder).val() == 0) {
                $(those.logs_container).find("pre").empty();
            }
            
            var finished_byte = response['finished_byte'];
            $(those.start_byte_holder).val(finished_byte);

            var log_content = response['log_content'];
            if (log_content == "") {
                return;
            }

            //var span = $("<span></span>").text(log_content);
            $(those.logs_container).find("pre").append(log_content);
        });
    },

    refresh_logs_with_interval: function() {
        var those = this;
        this.log_checker_timer_id = setInterval(function() {those.refresh_logs()}, 10000);
    },

    clear_interval: function() {
        clearInterval(this.log_checker_timer_id);
    }
};

var log_checker = new LogChecker();